<?php
namespace Home\Controller;

class CardController extends ParentController {
    protected function _initialize() {
        $this->hasRight = parent::isLogin();
        if (empty($this->hasRight)) {
            returnJson(403, 304);
            return;
        }
    }

    /**
     * 获取用户房卡列表接口
     * @modify trainyao 2016-05-11    创建
     */
    public function item() {
        if (empty($this->hasRight)) {
            return;
        }

        $userId = session(C('USER_ID'));
        if ($userId == 0) {
            returnJson(200, 200, array(
                'card' => array(),
            ));
            return;
        }

        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('CARD_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'userId' => $userId,
        );
        $result = $client->item($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            foreach ($result['data'] as &$cardItem) {
                $cardItem['startDate'] = $cardItem['startDate'];
                $cardItem['endDate'] = $cardItem['endDate'];
            }
            unset($cardItem);
            returnJson(200, 200, array(
                'card' => $result['data'],
            ));
            return;
        }

        returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
    }

    /**
     * 获取房卡数据接口
     * @modify trainyao 2016-05-17    创建
     * @modify wenhui 2017-02-20 修改 以子订单id获取房卡
     */
    public function detail() {
        if (empty($this->hasRight)) {
            return;
        }

        $userId = session(C('USER_ID'));
        $mobile = session(C('USER_MOBILE'));
        $areaCode = session(C('USER_AREACODE'));
        if ($userId == 0) {
            returnJson(403, 100, array());
            return;
        }

        // 调用RPC 获取对应钥匙包id
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('QRCLIENTSYNC_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'webUserId' =>$userId,
            'areaCode' => $areaCode,
            'mobile' => $mobile,
            'isSync' => false,
        );
        // 微信登录且未绑定手机号的 不需要请求
        if (!is_null($mobile) && !is_null($areaCode)) {
            $qrclientDate = $client->getPersonInfo($RPCData);
        }

        $param = I('post.');
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('CARD_RPC_URL'));
        $client->setEncryptMode(3);
        $RPCData = array(
            'flag' => C('MEIZHU_RPC_FLAG'),
            'time' => time(),
            'userId' => isset($qrclientDate['userlogin_id']) ? $qrclientDate['userlogin_id'] : 0,
            //'orderNumber' => $param['orderNumber'],
            'bookUserId' => $param['bookUserId'],
            'orderRoomId' => $param['orderRoomId'],
            'hotel' => $param['hotel'],
        );
        $result = $client->detail($RPCData);
        if (!is_array($result)) {
            returnJson(500, 500);
        }

        // 根据不同情况返回信息
        if ($result['httpstatus'] == 200) {
            returnJson(200, 200, $result['data']);
            return;
        } else if ($result['status'] == 601) {
            returnJson(403, 601, 'error');
            return;
        } else if ($result['status'] == 602) {
            returnJson(403, 602, 'error');
            return;
        } else if ($result['status'] == 603) {
            returnJson(403, 603, 'error');
            return;
        } else if ($result['status'] == 606) {
            returnJson(403, 606, 'error');
            return;
        }

        if (!empty($result['data'])) {
            returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info'], $result['data']);
        } else {
            returnJson(403, 101, $result['httpstatus'] . $result['status'], null, null, $result['info']);
        }
    }
}
